package com.orangelife.mobile.shoppingcart.biz;

import android.widget.ImageView;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.confirmOrder.activity.ShoppingConfirmOrderActivity;
import com.orangelife.mobile.shoppingcart.bean.ShoppingCartBean;
import com.orangelife.mobile.shoppingcart.db.ShoppingCartDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 绯若虚无 on 2015/10/12.
 */
public class ShoppingCartBiz {


    public static ArrayList<String> getSettleProductID(List<ShoppingCartBean> list) {

        //只要子项有一个被选中，就要传递MID；
        //如果子项全部没有被选中，那么就不要传MID；

        ArrayList<String> listMerIDs = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isInvalidList()) {//是否是失效列表
                continue;
            }
            for (int j = 0; j < list.get(i).getGoods().size(); j++) {
                boolean isSelected = list.get(i).getGoods().get(j).isChildSelected();
                if (isSelected) {
                    listMerIDs.add(list.get(i).getGoods().get(j).getProductID());
                }
            }
        }
        return listMerIDs;
    }

    /**
     * 获取结算信息
     *
     * @param list
     * @return
     */
    public static ArrayList<HashMap<String, Object>> getSettleInfo(List<ShoppingCartBean> list) {

        ArrayList<HashMap<String, Object>> listMerIDs = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isInvalidList()) {
                continue;
            }
            boolean isChildSelected = false;
            HashMap<String, Object> map = new HashMap<>();
            ArrayList<String> listProductIDs = new ArrayList<>();
            ArrayList<String> listProductNums = new ArrayList<>();
            ArrayList<String> listSellPloyIDs = new ArrayList<>();
            for (int j = 0; j < list.get(i).getGoods().size(); j++) {
                boolean isSelected = list.get(i).getGoods().get(j).isChildSelected();
                if (isSelected) {
                    isChildSelected = true;
                    listProductIDs.add(list.get(i).getGoods().get(j).getProductID());
                    listProductNums.add(list.get(i).getGoods().get(j).getNumber());
                    listSellPloyIDs.add(list.get(i).getGoods().get(j).getSellPloyID());
                }
            }
            if (isChildSelected) {//如果有子项被选中，那么就
                String mid = list.get(i).getMerID();
                map.put(ShoppingConfirmOrderActivity.MER_ID, mid);
                map.put(ShoppingConfirmOrderActivity.PRODUCT_IDS, listProductIDs);
                map.put(ShoppingConfirmOrderActivity.PRODUCT_NUMBER, listProductNums);
                map.put(ShoppingConfirmOrderActivity.SELLPLOY_ID, listSellPloyIDs);
                listMerIDs.add(map);
            }
        }
        return listMerIDs;
    }

    /**
     * 选择全部，点下全部按钮，改变所有商品选中状态
     */
    public static boolean selectAll(List<ShoppingCartBean> list, boolean isSelectAll, ImageView ivCheck) {
        isSelectAll = !isSelectAll;
        ShoppingCartBiz.checkItem(isSelectAll, ivCheck);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIsGroupSelected(isSelectAll);
            for (int j = 0; j < list.get(i).getGoods().size(); j++) {
                list.get(i).getGoods().get(j).setIsChildSelected(isSelectAll);
            }
        }
        return isSelectAll;
    }

    /**
     * 族内的所有组，是否都被选中，即全选
     *
     * @param list
     * @return
     */
    private static boolean isSelectAllGroup(List<ShoppingCartBean> list) {
        for (int i = 0; i < list.size(); i++) {
            boolean isAllGoodsInvalid = list.get(i).isAllGoodsInvalid();
            boolean isInvalidList = list.get(i).isInvalidList();
            boolean isSelectGroup = list.get(i).isGroupSelected();

            if (!isSelectGroup && !isAllGoodsInvalid && !isInvalidList) {
                return false;
            }
        }
        return true;
    }

    /**
     * 组内所有子选项是否全部被选中
     *
     * @param list
     * @return
     */
    private static boolean isSelectAllChild(List<ShoppingCartBean.Goods> list) {
        for (int i = 0; i < list.size(); i++) {
            boolean isSelectGroup = list.get(i).isChildSelected();
            if (!isSelectGroup) {
                return false;
            }
        }
        return true;
    }

    /**
     * 单选一个，需要判断整个组的标志，整个族的标志，是否被全选，取消，则
     * 除了选择全部和选择单个可以单独设置背景色，其他都是通过改变值，然后notify；
     *
     * @param list
     * @param groudPosition
     * @param childPosition
     * @return 是否选择全部
     */
    public static boolean selectOne(List<ShoppingCartBean> list, int groudPosition, int childPosition) {
        boolean isSelectAll;
        boolean isSelectedOne = !(list.get(groudPosition).getGoods().get(childPosition).isChildSelected());
        list.get(groudPosition).getGoods().get(childPosition).setIsChildSelected(isSelectedOne);//单个图标的处理
        boolean isSelectCurrentGroup = isSelectAllChild(list.get(groudPosition).getGoods());
        list.get(groudPosition).setIsGroupSelected(isSelectCurrentGroup);//组图标的处理
        isSelectAll = isSelectAllGroup(list);
        return isSelectAll;
    }

    public static boolean selectGroup(List<ShoppingCartBean> list, int groudPosition) {
        boolean isSelectAll;
        boolean isSelected = !(list.get(groudPosition).isGroupSelected());
        list.get(groudPosition).setIsGroupSelected(isSelected);
        for (int i = 0; i < list.get(groudPosition).getGoods().size(); i++) {
            list.get(groudPosition).getGoods().get(i).setIsChildSelected(isSelected);
        }
        isSelectAll = isSelectAllGroup(list);
        return isSelectAll;
    }

    /**
     * 勾与不勾选中选项
     *
     * @param isSelect 原先状态
     * @param ivCheck
     * @return 是否勾上，之后状态
     */
    public static boolean checkItem(boolean isSelect, ImageView ivCheck) {
        checkItem(isSelect, ivCheck, R.drawable.ic_checked, R.drawable.ic_uncheck);
        return isSelect;
    }

    public static boolean checkItemHouse(boolean isSelect, ImageView ivCheck) {
        checkItem(isSelect, ivCheck, R.drawable.icon_selected, R.drawable.icon_unselected);
        return isSelect;
    }

    public static boolean checkItem(boolean isSelect, ImageView ivCheck, int resCheck, int resUncheck) {
        if (isSelect) {
            ivCheck.setImageResource(resCheck);
        } else {
            ivCheck.setImageResource(resUncheck);
        }
        return isSelect;
    }

    /**
     * 添加某商品的数量到数据库
     *
     * @param productID 此商品的规格ID
     * @param num       此商品的数量
     * @return 是否重复
     */
    public static void saveGoodNum(String productID, String num) {
        ShoppingCartDao.getInstance().saveShoppingInfo(productID, num);
    }

    /**
     * 此商品是否已经存在购物车中
     *
     * @param productID 规格ID
     * @return 是否存在
     */
    public static boolean isProductExist(String productID) {
        return ShoppingCartDao.getInstance().isExistGood(productID);
    }

    public static void delInvalidList(ArrayList<ShoppingCartBean.Goods> listInvalid) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < listInvalid.size(); i++) {
            list.add(listInvalid.get(i).getProductID());
        }
        ShoppingCartDao.getInstance().deleteGoodList(list);
    }

    public static void delGoodsList(ArrayList<String> listGoods) {
        ShoppingCartDao.getInstance().deleteGoodList(listGoods);
    }

    /**
     * 删除某个商品,即删除其ProductID
     *
     * @param productID 规格ID
     */
    public static void delGood(String productID) {
        ShoppingCartDao.getInstance().deleteShoppingInfo(productID);
    }

    public static void calcuGoodsNum(boolean isPlus, ShoppingCartBean.Goods goods, TextView tvNum) {
        String currentNum = goods.getNumber().trim();
        String num = "1";
        if (isPlus) {
            num = String.valueOf(Integer.parseInt(currentNum) + 1);
        } else {
            int i = Integer.parseInt(currentNum);
            if (i > 1) {
                num = String.valueOf(i - 1);
            } else {
                num = "1";
            }
        }
        String productID = goods.getProductID();
        tvNum.setText(num);
        goods.setNumber(num);
        //存到SP里的，没有X
        updateSP(productID, num);
    }

    /**
     * 更新购物车的单个商品数量
     *
     * @param productID
     * @param num
     */
    public static void updateSP(String productID, String num) {
        ShoppingCartDao.getInstance().updateGoodsNum(productID, num);
    }

    /**
     * 查询购物车商品总数量
     *
     * @return
     */
    public static int getGoodsCount() {
        return ShoppingCartDao.getInstance().getGoodsCount();
    }

    public static List<String> getAllProductID() {
        return ShoppingCartDao.getInstance().getProductList();
    }

    public static void updateShopList(List<ShoppingCartBean> list) {
        if (list == null) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            ShoppingCartBean scb = list.get(i);
            if (scb == null) {
                continue;
            }
            ArrayList<ShoppingCartBean.Goods> list2 = scb.getGoods();
            if (list2 == null) {
                continue;
            }
            for (int j = 0; j < list2.size(); j++) {
                ShoppingCartBean.Goods goods = list2.get(j);
                if (goods == null) {
                    continue;
                }
                String productID = goods.getProductID();
                String num = ShoppingCartDao.getInstance().getNumByProductID(productID);
                list.get(i).getGoods().get(j).setNumber(num);
            }
        }
    }

}
